//
//  AGGetBookInfo.m
//  AGPastime
//
//  Created by Ager on 15/11/30.
//  Copyright © 2015年 Ager. All rights reserved.
//

#import "AGGetBookInfo.h"
#import "AGBookModel.h"

@implementation AGGetBookInfo

+ (NSDictionary *)getJingpinBook{
    
    
    NSMutableArray *dataArray = [NSMutableArray array];
    NSMutableArray *headArray = [NSMutableArray array];
    

    NSURL *url1 = [NSURL URLWithString:kBookTuijian];
    
    NSURLRequest *request1 = [NSURLRequest requestWithURL:url1];
    
    AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request1];
    
    [operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSMutableArray *array = [NSMutableArray array];
        
        NSDictionary *d = [[dic objectForKey:@"list"] objectAtIndex:0];
        
        [headArray addObject:[d objectForKey:@"phName"]];
        
        for (NSDictionary *dictionry in [d objectForKey:@"books"]) {
            AGBookModel *model = [[AGBookModel alloc]init];
            [model setValuesForKeysWithDictionary:dictionry];
            [array addObject:model];
        }
        [dataArray addObject:array];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"Error: %@", error);
        
    }];
    
    NSURL *url2 = [NSURL URLWithString:kBookYanqing];
    
    NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
    
    AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request2];
    
    [operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSMutableArray *array = [NSMutableArray array];
        
        NSDictionary *d = [[dic objectForKey:@"list"] objectAtIndex:0];
        
        [headArray addObject:[d objectForKey:@"phName"]];
        
        for (NSDictionary *dictionry in [d objectForKey:@"books"]) {
            AGBookModel *model = [[AGBookModel alloc]init];
            [model setValuesForKeysWithDictionary:dictionry];
            [array addObject:model];
        }
        [dataArray addObject:array];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"Error: %@", error);
        
    }];

    NSURL *url3 = [NSURL URLWithString:kBookQixia];
    
    NSURLRequest *request3 = [NSURLRequest requestWithURL:url3];
    
    AFHTTPRequestOperation *operation3 = [[AFHTTPRequestOperation alloc] initWithRequest:request3];
    
    [operation3 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSMutableArray *array = [NSMutableArray array];
        
        NSDictionary *d = [[dic objectForKey:@"list"] objectAtIndex:0];
        
        [headArray addObject:[d objectForKey:@"phName"]];
        
        for (NSDictionary *dictionry in [d objectForKey:@"books"]) {
            AGBookModel *model = [[AGBookModel alloc]init];
            [model setValuesForKeysWithDictionary:dictionry];
            [array addObject:model];
        }
        [dataArray addObject:array];

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"Error: %@", error);
        
    }];

    NSURL *url4 = [NSURL URLWithString:kBookLishi];
    
    NSURLRequest *request4 = [NSURLRequest requestWithURL:url4];
    
    AFHTTPRequestOperation *operation4 = [[AFHTTPRequestOperation alloc] initWithRequest:request4];
    
    [operation4 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSMutableArray *array = [NSMutableArray array];
        
        NSDictionary *d = [[dic objectForKey:@"list"] objectAtIndex:0];
        
        [headArray addObject:[d objectForKey:@"phName"]];
        
        for (NSDictionary *dictionry in [d objectForKey:@"books"]) {
            AGBookModel *model = [[AGBookModel alloc]init];
            [model setValuesForKeysWithDictionary:dictionry];
            [array addObject:model];
        }
        [dataArray addObject:array];

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"Error: %@", error);
        
    }];

    
    NSURL *url5 = [NSURL URLWithString:kBookNew];
    
    NSURLRequest *request5 = [NSURLRequest requestWithURL:url5];
    
    AFHTTPRequestOperation *operation5 = [[AFHTTPRequestOperation alloc] initWithRequest:request5];
    
    [operation5 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSMutableArray *array = [NSMutableArray array];
        
        NSDictionary *d = [[dic objectForKey:@"list"] objectAtIndex:0];
        
        [headArray addObject:[d objectForKey:@"phName"]];
        
        for (NSDictionary *dictionry in [d objectForKey:@"books"]) {
            AGBookModel *model = [[AGBookModel alloc]init];
            [model setValuesForKeysWithDictionary:dictionry];
            [array addObject:model];
        }
        [dataArray addObject:array];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"Error: %@", error);
        
    }];

    
    //operation2 在 operation1 请求完成后执行
    
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    
    [operation2 addDependency:operation1];
    [operation3 addDependency:operation2];
    [operation4 addDependency:operation3];
    [operation5 addDependency:operation4];
    
    [operationQueue addOperations:@[operation1, operation2, operation3,operation4,operation5] waitUntilFinished:YES];
    
    
    NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:headArray,@"head",dataArray,@"data" ,nil];
    
    
    return result;
    
}


@end
